---
title: Memory Management Basics
---

Memory management is a critical aspect of C++ programming, offering fine-grained control over how memory is allocated and deallocated. This is a significant departure from JavaScript's automatic garbage collection.

## Stack Memory vs. Heap Memory

In C++, memory is primarily managed in two regions: the stack and the heap.

### Stack Memory
*   **Automatic Allocation:** Used for local variables, function parameters, and return addresses.
*   **LIFO (Last-In, First-Out):** Memory is allocated and deallocated in a strict order.
*   **Fast Access:** Very fast allocation and deallocation.
*   **Limited Size:** Typically smaller than heap memory.
*   **Lifetime:** Variables on the stack are automatically deallocated when they go out of scope.

<UniversalEditor title="Stack Memory Example" compare={true}>
```javascript !! js
// JavaScript: Stack-like behavior for primitive types and function calls
function calculateSum(a, b) {
  let result = a + b; // 'result' is conceptually on the stack frame of calculateSum
  return result;
}

let x = 10; // 'x' is on the stack
let y = 20; // 'y' is on the stack
let sum = calculateSum(x, y); // Function call adds a new stack frame
```

```cpp !! cpp
// C++: Stack Memory
#include <iostream>

int calculateSum(int a, int b) {
  int result = a + b; // 'result' is allocated on the stack
  return result;
} // 'result' is deallocated when calculateSum returns

int main() {
  int x = 10; // 'x' is allocated on the stack
  int y = 20; // 'y' is allocated on the stack
  int sum = calculateSum(x, y); // calculateSum adds a stack frame
  // std::cout << "Sum: " << sum << std::endl;
  return 0;
} // 'x', 'y', 'sum' are deallocated when main returns
```
</UniversalEditor>

### Heap Memory
*   **Dynamic Allocation:** Used for data whose size is not known at compile time or whose lifetime extends beyond the scope of a function.
*   **Manual Management:** Programmers must explicitly allocate and deallocate memory using `new` and `delete` (or `malloc`/`free`).
*   **Slower Access:** Allocation and deallocation are slower than stack memory.
*   **Larger Size:** Typically much larger than stack memory.
*   **Lifetime:** Memory persists until explicitly deallocated or the program terminates.

<UniversalEditor title="Heap Memory Example" compare={true}>
```javascript !! js
// JavaScript: Objects are allocated on the heap
let obj = { name: "Alice" }; // 'obj' reference is on stack, object data on heap

// When 'obj' is no longer referenced, garbage collector reclaims memory
obj = null; 
```

```cpp !! cpp
// C++: Heap Memory
#include <iostream>

int main() {
  int* dynamicInt = new int; // Allocate an integer on the heap
  *dynamicInt = 100;
  // std::cout << "Dynamic Int: " << *dynamicInt << std::endl;

  delete dynamicInt; // Deallocate the memory
  dynamicInt = nullptr; // Good practice to set pointer to nullptr after deletion

  // Allocate an array on the heap
  int* dynamicArray = new int[5];
  for (int i = 0; i < 5; ++i) {
    dynamicArray[i] = i * 10;
  }
  // Remember to delete[] for arrays
  delete[] dynamicArray;
  dynamicArray = nullptr;

  return 0;
}
```
</UniversalEditor>

## Variable Lifetime

*   **Automatic (Stack) Variables:** Created when their scope is entered and destroyed when their scope is exited.
*   **Dynamic (Heap) Variables:** Created with `new` and persist until explicitly `delete`d.
*   **Static Variables:** Allocated once at program startup and persist for the entire duration of the program.
*   **Global Variables:** Similar to static variables, allocated at program startup and persist for the program's lifetime.

## Memory Layout Basics

A typical C++ program's memory layout includes:
*   **Text/Code Segment:** Stores the executable instructions.
*   **Data Segment:** Stores global and static variables (initialized and uninitialized).
*   **Heap:** Dynamically allocated memory.
*   **Stack:** Local variables and function call information.

## Comparison with JavaScript Memory Model

JavaScript uses a **garbage collector (GC)** to automatically manage memory. When an object is no longer reachable (no longer referenced by any part of the program), the GC automatically reclaims the memory. This frees developers from manual memory management, reducing common errors like memory leaks and dangling pointers.

C++, on the other hand, requires **manual memory management**. Developers are responsible for allocating memory when needed and deallocating it when no longer required. Failure to deallocate memory leads to **memory leaks**, and attempting to access deallocated memory leads to **dangling pointers** or **use-after-free** errors.

## Memory Leak Concepts

A **memory leak** occurs when a program allocates memory on the heap but fails to deallocate it when it's no longer needed. This leads to a gradual consumption of available memory, potentially causing the program or even the entire system to slow down or crash.

**Common causes of memory leaks in C++:**
*   Forgetting to `delete` memory allocated with `new`.
*   Losing the pointer to dynamically allocated memory.
*   Incorrectly handling memory in error paths or exceptions.

## Basic Memory Management Principles

1.  **Allocate when needed, deallocate when done:** Every `new` must have a corresponding `delete` (or `new[]` with `delete[]`).
2.  **Ownership:** Clearly define which part of the code is responsible for managing a piece of memory.
3.  **RAII (Resource Acquisition Is Initialization):** A C++ programming idiom that ties the lifetime of a resource (like memory) to the lifetime of an object. When the object goes out of scope, its destructor automatically releases the resource. Smart pointers are a prime example of RAII.

## Debugging Memory Issues

Debugging memory issues in C++ can be challenging. Tools commonly used include:
*   **Valgrind (Linux/macOS):** A powerful memory debugger that can detect memory leaks, use-after-free errors, and other memory-related problems.
*   **AddressSanitizer (Clang/GCC):** A fast memory error detector integrated into compilers.
*   **Visual Studio Diagnostic Tools (Windows):** Built-in tools for memory profiling and leak detection.

---

### Practice Questions:
1.  Explain the key differences between stack and heap memory in C++. When would you choose to allocate memory on the heap instead of the stack?
2.  What is a memory leak, and how can it occur in C++? How does JavaScript's memory management approach mitigate this issue?
3.  Describe the RAII principle and how it helps in C++ memory management.

### Project Idea:
*   Write a C++ program that simulates a simple linked list. Implement functions to add nodes, remove nodes, and print the list. Pay close attention to memory allocation and deallocation to avoid memory leaks. Use `new` and `delete` explicitly.
